package com.yic.module.base.service.supplier;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.dal.dataobject.brand.BrandDO;
import com.yic.module.base.dal.redis.RedisKeyConstants;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.base.controller.admin.supplier.vo.*;
import com.yic.module.base.dal.dataobject.supplier.SupplierDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.base.convert.supplier.SupplierConvert;
import com.yic.module.base.dal.mysql.supplier.SupplierMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.base.enums.ErrorCodeConstants.*;
import static com.yic.module.base.enums.ErrorCodeConstants.SUPPLIER_CODE_EXISTS;

/**
 * 供应商信息 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class SupplierServiceImpl implements SupplierService {

    @Resource
    private SupplierMapper supplierMapper;

    @Override
    public Long createSupplier(SupplierCreateReqVO createReqVO) {
        validateCreateOrUpdate(null, createReqVO.getName(), createReqVO.getCode());
        // 插入
        SupplierDO supplier = SupplierConvert.INSTANCE.convert(createReqVO);
        supplierMapper.insert(supplier);
        // 返回
        return supplier.getId();
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.SUPPLIER, key = "#updateReqVO.id")
    public void updateSupplier(SupplierUpdateReqVO updateReqVO) {
        // 校验
        validateCreateOrUpdate(updateReqVO.getId(), updateReqVO.getName(), updateReqVO.getCode());
        // 更新
        SupplierDO updateObj = SupplierConvert.INSTANCE.convert(updateReqVO);
        supplierMapper.updateById(updateObj);
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.SUPPLIER, key = "#id")
    public void deleteSupplier(Long id) {
        // 校验存在
        validateSupplierExists(id);
        // 删除
        supplierMapper.deleteById(id);
    }

    private void validateCreateOrUpdate(Long id, String name, String code){
        validateNameExists(id, name);
        validateCodeExist(id, code);
    }

    private void validateNameExists(Long id, String name) {
        SupplierDO supplier = supplierMapper.selectByName(name);
        if (ObjectUtil.isEmpty(supplier)){
            return;
        }
        if (id == null){
            throw exception(SUPPLIER_NAME_EXISTS);
        }
        if (!id.equals(supplier.getId())){
            throw exception(SUPPLIER_NAME_EXISTS);
        }
    }

    private void validateCodeExist(Long id, String code){
        SupplierDO supplier = supplierMapper.selectByCode(code);
        if (ObjectUtil.isEmpty(supplier)){
            return;
        }
        if (id == null){
            throw exception(SUPPLIER_CODE_EXISTS);
        }
        if (!id.equals(supplier.getId())){
            throw exception(SUPPLIER_CODE_EXISTS);
        }
    }

    private void validateSupplierExists(Long id) {
        if (supplierMapper.selectById(id) == null) {
            throw exception(SUPPLIER_NOT_EXISTS);
        }
    }

    @Override
    @Cacheable(cacheNames = RedisKeyConstants.SUPPLIER, key = "#id")
    public SupplierDO getSupplier(Long id) {
        return supplierMapper.selectById(id);
    }

    @Override
    public List<SupplierDO> getSupplierList(Collection<Long> ids) {
        return supplierMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SupplierDO> getSupplierPage(SupplierPageReqVO pageReqVO) {
        return supplierMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SupplierDO> getSupplierList(SupplierExportReqVO exportReqVO) {
        return supplierMapper.selectList(exportReqVO);
    }

    @Override
    public List<SupplierDO> getSupplierList(Collection<Long> ids, Collection<Integer> statuses) {
        return supplierMapper.selectList(ids, statuses);
    }

}
